package net.seehealth.healthpad.application.util;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Random;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;

/**
 * 公共工具类
 * @author赵文斌
 *
 */

public class CommonUtil {
	
	/**
	 * 判断对象是否为空
	 * @param object 当前对象
	 * @return true:为空  false:不为空
	 */
	public static boolean isEmpty(Object object) {
		if (object == null)
			return true;
		if (object instanceof CharSequence)
			return ((CharSequence) object).length() == 0;
		if (object instanceof Collection)
			return ((Collection<?>) object).isEmpty();
		if (object instanceof Map)
			return ((Map<?,?>) object).isEmpty();
		if (object.getClass().isArray())
			return java.lang.reflect.Array.getLength(object) == 0;
		return false;
	}

	/**
	 * 判断对象是否不为空
	 * @param object 当前对象
	 * @return true:不为空  false:为空
	 */
	public static boolean isNotEmpty(Object object) {
		return !isEmpty(object);
	}

	/**
	 * 判断当前界面是否是桌面   
	 * @param context 当前上下文
	 * @return true：桌面 false：非桌面
	 */
	public static boolean isHome(Context context){
		ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);  
		List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);  
		return tasks.get(0).baseActivity.getPackageName().contains("launcher");
	}
	
	/***
	 * 随机数的随机生成
	 * 
	 * @return
	 */
	public static short genRandom() {
		short result;
		try {
			Random rand = new Random();
			result = (short) rand.nextInt();
		} catch (Exception e) {
			Random rd = new Random();
			result = (short) rd.nextInt();
		}
		if (result < 0) {
			result = (short) (-result);
		}
		return result;
	}
	
	/**
	 * 发送Email
	 * @param context 当前上下文
	 * @param email	email地址
	 */
//	public static void sendEmail(Context context, String email){
//		if(StringUtil.isEmpty(email)) {
//			return;
//		}
//		Uri uri = Uri.parse("mailto:" + email);
//		Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
//		context.startActivity(intent);
//	}

	/**发送电子邮件
	 * @param context 当前上下文
	 * @param email	email地址
	 * @return
	 */
	public static void sendMailByIntent(Context mContext, String email) {
		if(StringUtil.isEmpty(email)) {
			return;
		}
        String[] reciver = new String[] { email };  
        String[] mySbuject = new String[] { "" };  
        String myCc = "";  
        String mybody = "";  
        Intent myIntent = new Intent(android.content.Intent.ACTION_SEND);  
        myIntent.setType("text/plain");  
        myIntent.putExtra(android.content.Intent.EXTRA_EMAIL, reciver);  
        myIntent.putExtra(android.content.Intent.EXTRA_CC, myCc);  
        myIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, mySbuject);  
        myIntent.putExtra(android.content.Intent.EXTRA_TEXT, mybody);  
        try {
//        	mContext.startActivity(Intent.createChooser(myIntent,
//        			mContext.getResources().getString(R.string.select_email_client)));
        } catch(Exception ex) {
//        	showToast(mContext.getResources().getString(R.string.install_email_client), mContext);
        }
    }  
}
